import { Injectable, Inject, BadRequestException } from '@nestjs/common';
import { PrismaService } from "src/prisma/prisma.service";
import { FriendAddDto } from "./dto/friend-add.dto";

@Injectable()
export class FriendshipService {

    @Inject(PrismaService)
    private prismaService: PrismaService;

    async add(friendAddDto: FriendAddDto, userId: number) {
        // 1.判断添加的好友存不存在
        const friend = await this.prismaService.user.findUnique({
            where: {
                username: friendAddDto.username
            }
        });
        if (!friend) throw new BadRequestException('要添加的好友不存在');
        if (friend.id === userId) throw new BadRequestException('不能添加自己为好友');

        return await this.prismaService.friendRequest.create({
            data: {
                fromUserId: userId,
                toUserId: friend.id,
                reason: friendAddDto.reason,
                status: 0
            }
        })
    }
    // 获取好友申请，不仅要获取我发出的好友申请，还要获取到别人发给我的好友申请；还需要带出申请的用户信息
    async list(userId: number) {
        const fromMeRequest = await this.prismaService.friendRequest.findMany({
            where: {
                fromUserId: userId
            }
        });
        const toMeRequest = await this.prismaService.friendRequest.findMany({
            where: {
                toUserId: userId
            }
        });

        const res = {
            toMe: [],
            fromMe: []
        }

        for (let i = 0; i < fromMeRequest.length; i++) {
            const user = await this.prismaService.user.findUnique({
                where: {
                    id: fromMeRequest[i].toUserId
                },
                select: {
                    id: true,
                    username: true,
                    nickName: true,
                    email: true,
                    headPic: true,
                    createTime: true
                }
            });
            res.fromMe.push({
                ...fromMeRequest[i],
                toUser: user
            });
        }

        for (let i = 0; i < toMeRequest.length; i++) {
            const user = await this.prismaService.user.findUnique({
                where: {
                    id: toMeRequest[i].fromUserId
                },
                select: {
                    id: true,
                    username: true,
                    nickName: true,
                    email: true,
                    headPic: true,
                    createTime: true
                }
            });

            res.toMe.push({
                ...toMeRequest[i],
                fromUser: user
            });
        }

        return res;
    }

    async agree(friendId: number, userId: number) {
        // 1.更改好友申请表状态值
        await this.prismaService.friendRequest.updateMany({
            where: {
                fromUserId: friendId,
                toUserId: userId,
                status: 0
            },
            data: {
                status: 1
            }
        });
        // 2.同时需要往好友关系表添加一条数据(添加前，需要判断是否已经是好友了)
        const res = await this.prismaService.friendship.findMany({
            where: {
                userId,
                friendId
            }
        });
        if (!res.length) {
            await this.prismaService.friendship.create({
                data: {
                    friendId,
                    userId
                }
            })
        }
        return "好友添加成功";
    }

    async reject(friendId: number, userId: number) {
        await this.prismaService.friendRequest.updateMany({
            where: {
                fromUserId: friendId,
                toUserId: userId,
                status: 0,
            },
            data: {
                status: 2
            }
        });

        return "已拒绝";
    }

    async remove(friendId: number, userId: number) {
        await this.prismaService.friendship.deleteMany({
            where: {
                friendId,
                userId
            }
        });

        return "删除成功";
    }
}
